<!DOCTYPE html>
<html>
    <header>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no,maximum-scale=1.0,minimum-scale=1.0">
        <title>
            注册-编辑用户信息
        </title>
        <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    
        </script>
        <!-- 引入 layui.css -->
    <link href="./css/layui.css" rel="stylesheet" />

    <!-- 引入 layui.js -->
    <script src="./js/layui.js"></script>
    <script src="./js/utils.js"></script>
    <script src="./js/constant.js" ></script>
    <script src="./js/data.js"></script>
    <script src="./js/userAuth.js"></script>
    <script src="./js/request.js"></script>
        <script type="text/javascript">
            
            function onSubmit(){
                toAuth();
            }

            function toAuth(){

                // savergb(229,86,77)irect(AUTH_COMPANY_PAGE);
                // location.replace(MINE_PAGE);
                // return ;
                
                const name = $("#name").val();
                const idNumber = $("#idNumber").val();
                const mobile = $("#mobile").val();
                const code = $("#code").val();
                const check   = $("#read").is(":checked");
                if(name==""){
                    layer.msg("姓名不能为空")
                    return;
                }
                if(!idnumberIsValid(idNumber)){
                    console.log(idNumber)
                    layer.msg("身份证不合法");
                    return;
                }
                if(receiveMobile != $("#mobile").val()){
                    layer.msg("接收短信手机与当前手机不符，请重新输入")
                    return ;
                }
                if(!mobileIsValid(mobile)){
                    layer.msg("手机号不合法");
                    return ;
                }
                if(code==""){
                    layer.msg("请输入验证码");
                    return ;
                }
                if(!check){
                    layer.msg("请先阅读并勾选协议")
                    return;
                }

                const info = getRegisterInfo();
                const wxUserInfo = getUserInfo();
                
                // 提交用户信息
                const param = {
                    UserName : name,
                    IDNumber : idNumber,
                    phoneNumber : mobile,
                    HeadImage : wxUserInfo.headimgurl || "",
                    IDNumberUrl1 : info.idCardFront,
                    IDNumberUrl2 : info.idCardBack,
                    ValidCode : code,
                    action:"CreateUser"
                }
                $.ajax({
                    url : REQUEST_URL,
                    type : "post",
                    contentType:"application/json",
                    headers:{
                        ...REQUEST_HEADERS(),
                        ValidCode : getCodeValidation(),
                    },
                    data : JSON.stringify(param),
                    success:function(r,t,x){
                        const res = responseHandler(r,t,x);
                        if(res.code == 200){
                            //注册成功之后绑定用户，使用注册时使用的短信验证码即可？（这可能是个bug）
                            layer.msg("注册成功，正在自动绑定")
                            $.ajax({
                                url : REQUEST_URL,
                                data : JSON.stringify({action : "BindeUser",PhoneNumber : mobile,HeadImage:wxUserInfo.headimgurl||"",ValidCode:code}),
                                headers:  {
                                    ...REQUEST_HEADERS(),
                                    ValidCode : getCodeValidation(),
                                },
                                type : "post",
                                contentType : "application/json",
                                success:function(r,t,x){
                                    if(r.code == 200){
                                        layer.msg("绑定成功，即将自动跳转");
                                        clearRegisterInfo();
                                        saveRedirect(AUTH_COMPANY_PAGE);
                                        location.replace(MINE_PAGE);
                                    }else{
                                        layer.msg("绑定失败，请手工绑定");
                                        setTimeout(() => {
                                            location.replace(LOGIN_PAGE);
                                        }, 2000);
                                    }
                                }
                            })


                        }else{
                            layer.msg(res.message);
                        }
                    }
                })
                // 删除本地注册信息
            }

            // function bindUser(){
            //     $.ajax({
            //         url : REQUEST_URL,
            //     method:"post",
            //     type:"json",
            //     contentType:"application/json",
            //     headers:{
            //         ...REQUEST_HEADERS(),
            //         ValidCode:getCodeValidation()
            //     },
            //     data: JSON.stringify({
            //         action : "BindeUser",
            //         phoneNumber:account,
            //         HeadImage:userInfo?userInfo.headimgurl : "",
            //         ValidCode:pwd
            //     }),
            //     })
            // }

            function clickBack(){
                history.go()
            }

            var countDownVal = -1
            var receiveMobile = "";
            function getCode(){
                if(countDownVal > 0){
                    return ;
                }else{
                    const mobile = $("#mobile").val();
                    if(mobile.length!=11){
                        layer.msg("请输入合法手机号码");
                        return ;
                    }
                    receiveMobile = mobile ;
                    const loadIndex= layer.load();
                    $.ajax({
                        url : REQUEST_SEND_CODE+"?PhoneNumber="+mobile,
                        type : "get",
                        headers : REQUEST_HEADERS(),
                        success:function(r,t,x){
                            layer.close(loadIndex)
                            const res = responseHandler(r,t,x);
                            if(res.code == 200){
                                layer.msg("信息已发送")
                                saveCodeValidation(res.validCode);
                                countDownVal = 60;
                                countDown();
                            }else{
                                layer.msg(r.message);
                            }
                        },
                        error:function(a,e,s){
                            layer.close(loadIndex)
                            layer.msg(s)
                        }

                    })
                    
                }
                

            }
            function countDown(){
                const timer = setInterval(() => {
                    countDownVal = countDownVal - 1;
                    if(countDownVal<0){
                        clearInterval(timer);
                        $("#getCode").text("获取验证码");
                        $("#getCode").css({"color":"rgb(231, 85, 72)"});
                    }else{
                        $("#getCode").text(countDownVal+"秒后继续")
                        $("#getCode").css({"color":"gray"})
                    }
                }, 1000);
            }

            $(document).ready(()=>{
                // init(false,true);
                whiteListPage();
                const registerInfo = getRegisterInfo();
                if(registerInfo && registerInfo.faceData){
                    const {idNumber,name} = registerInfo.faceData.data.face.data;
                    $("#name").val(name);
                    $("#idNumber").val(idNumber);
                }
                $("#getCode").click(function(){
                    getCode();
                })
            })
        </script>
    </header>
    <style>
        body {
            background-color: rgb(235,235,235);
        }
        .steps {
                /* width:  100%; */
                display: flex;
                flex-direction: row;
                align-items: center;
                padding: 0px 20px;
                margin-top: 20px;
            }
            .active-title {
                color:rgb(231, 85, 72);
            }
            .disactive-title {
                color: grey;
            }
            .line {
                height: 1px;
                /* background-color: rgb(220,220,220); */
                background-color: grey;
                display: flex;
                flex:1;
            }
            .active-line{
                height: 1px;
                /* background-color: rgb(220,220,220); */
                background-color: rgb(231, 85, 72);;
                display: flex;
                flex:1;
            }

            .header {
                text-align: center;
                padding: 20px;
                font-size: 18px;
                color: grey;
            }
            .form {
                background-color: #f4f4f4;
    border: 1px solid #ebebeb;
    margin: 0px 10px;
    padding: 15px;
    border-radius: 10px;
    display: flex;
    flex: 1 1;
    flex-direction: column;
            }
            .padding {
                margin-bottom: 10px;
            }
    </style>
    <body>
      <!-- 步骤条 -->
      <div class="steps">
        <div class="active-title">
         上传资料
        </div>
        <div class="active-line">
 
        </div>
        <div class="active-title">
         填写信息
        </div>
        <div class="line">
         
         </div>
        <div class="disactive-title">
         授权公司
        </div>
    </div>
 
    <div class="header">
     请填写您的基本信息
    </div>



    <div  class="form">
        <input id="name" type="text" name="title" requirgb(229,86,77)  lay-verify="requirgb(229,86,77)" placeholder="请输入您的姓名（自动识别）" autocomplete="off" class="layui-input padding">
        <input id="idNumber" type="text" name="title" requirgb(229,86,77)  lay-verify="requirgb(229,86,77)" placeholder="请输入您的身份证号（自动识别）" autocomplete="off" class="layui-input padding">
        <input id="mobile" type="text" name="title" requirgb(229,86,77)  lay-verify="requirgb(229,86,77)" placeholder="请输入您的手机号" autocomplete="off" class="layui-input padding">
        <div style="display: flex;flex-direction:row;align-items: center;" class="padding">
        <input id="code" type="text" name="title" requirgb(229,86,77)  lay-verify="requirgb(229,86,77)" placeholder="请输入验证码" autocomplete="off" class="layui-input">
        <a id="getCode" style="width:130px;color:rgb(231, 85, 72);padding: 0px 5px;text-align: center;">获取验证码</a>
        </div>
        <div>
        <input id="read" type="checkbox" name="like[write]" ></input>
        <span>
            阅读并同意<a style="    color: rgb(103, 167, 251);" href="privateLicense.html">
                《用户协议及隐私条例》
            </a>
        </span>
    </div>
    </div>

    <div style="position:absolute;width:100%;height:50px;display:flex;flex-direction:row;left:0px;bottom:0px">
        <span onclick="clickBack()" style="display: flex;flex:1;text-align: center;line-height: 50px;color:white;background-color: rgb(231, 85, 72);justify-content: center;">
            上一步
        </span>
        <span onclick="onSubmit()" style="display: flex;flex:1;text-align: center;line-height: 50px;color:white;background-color: rgb(231, 85, 72);justify-content: center;">
            提交注册
        </span>
    </div>

    </body>
</html>